<?php

namespace App\Repositories;

use App\Models\Product;
use InfyOm\Generator\Common\BaseRepository;

/**
 * Class ProductRepository
 * @package App\Repositories
 * @version November 28, 2017, 4:46 pm CST
 *
 * @method Product findWithoutFail($id, $columns = ['*'])
 * @method Product find($id, $columns = ['*'])
 * @method Product first($columns = ['*'])
 */
class ProductRepository extends BaseRepository
{
    /**
     * @var array
     */
    protected $fieldSearchable = [

    ];

    /**
     * Configure the Model
     **/
    public function model()
    {
        return Product::class;
    }

    public function getProductList($page_limit, $condition)
    {
        $desc = empty($condition['desc']) ? null : $condition['desc'];
        $asc = empty($condition['asc']) ? null : $condition['asc'];
        $list = Product::where(function ($query) use ($condition) {
            if (!empty($condition['ProductName'])) {
                $query->where('ProductName', 'like', '%' . $condition['ProductName'] . '%');
            }
        })->when($desc, function ($query) use ($desc) {
            $query->orderBy($desc, 'desc');
        })->when($asc, function ($query) use ($asc) {
            $query->orderBy($asc, 'asc');
        })->paginate($page_limit);
        return $list;
    }
}
